package com.beep.erp.model.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.beep.erp.Constants;
import com.beep.erp.MainActivity;
import com.beep.erp.R;
import com.beep.erp.common.base.BaseBean;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.base.simpleactivity.SimpleBackActivity;
import com.beep.erp.common.base.simpleactivity.SimpleBackPage;
import com.beep.erp.common.bean.LoginBean;
import com.beep.erp.common.utils.SPUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.beep.erp.AppContext._context;


public class LoginActivity extends ToolbarActivity<LoginPresenter, LoginModel> implements LoginContract.View, TextWatcher {


    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.btn_login)
    Button btn_login;


    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected boolean isBack() {
        return false;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.login);
        et_password.addTextChangedListener(this);
        et_username.addTextChangedListener(this);
        et_username.setText(new SPUtils(this, Constants.EXTRA_NAME).getString(Constants.USER_NAME, ""));

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!et_username.getText().toString().equals("") && !et_password.getText().toString().equals("")) {
            btn_login.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            btn_login.setTextColor(Color.parseColor("#66FFFFFF"));
        }
    }

    @OnClick({R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (ToolsUtils.isEmpty(et_username.getText().toString())) {
                    ToastUtils.showShort(getString(R.string.user_name_empty_tip));
                    return;
                }
                if (ToolsUtils.isEmpty(et_password.getText().toString())) {
                    ToastUtils.showShort(getString(R.string.pwd_empty_tip));
                    return;
                }
                mPresenter.login(et_username.getText().toString(), et_password.getText().toString());
                break;
        }
    }

    @Override
    public void showLoading() {
        showWaitDialog(getString(R.string.login_loading));
    }

    @Override
    public void hideLoading() {
        hideWaitDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }


    @Override
    public void login(LoginBean loginBean) {
        ToastUtils.showShort("登录成功");
        SPUtils spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);
        spUtils.putString(Constants.USER_NAME, loginBean.getLoginName());
        spUtils.putString(Constants.EXTRA_NAME, loginBean.getName());
        spUtils.putString(Constants.USER_ID, loginBean.getId());
        spUtils.putString(Constants.USER_PWD, loginBean.getPasswordMing());
        spUtils.putString(Constants.USER_PWD_MI, loginBean.getPasswordMi());
        spUtils.putBoolean(Constants.EXTRA_ISLOGIN, true);
        MainActivity.start(this);
    }
}
